<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">学生考勤列表</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-u-sm-12 am-form ">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.areaTeamId" :options="areaTeams">
                  <option value="" disabled>区域</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.busTeamId" :options="busTeams">
                  <option value="">业务组</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.periodId" :options="periods">
                  <option value="" disabled>期数</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.gradeId" :options="grades">
                  <option value="">年级</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.subjectId" :options="subjects">
                  <option value="">科目</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="className" v-model="query.operator" placeholder="操作人"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="className" v-model="query.studentName" placeholder="学生姓名"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="className" v-model="query.toClassName" placeholder="新班级名称"/>
              </div>
            </div>


            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <el-date-picker
                  v-model="query.startDate"
                  type="date"
                  placeholder="操作日期(开始)">
                </el-date-picker>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <el-date-picker
                  v-model="query.endDate"
                  type="date"
                  placeholder="操作日期(结束)">
                </el-date-picker>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="search" ><span class="am-icon-search"></span>查询
                </button>
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="handleExport" ><span class="am-icon-search"></span>导出
                </button>
              </div>
            </div>


          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                prop="studentName"
                label="学生姓名"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="operator"
                label="操作人"
                min-width="200">
              </el-table-column>
              <el-table-column
                label="调课操作时间"
                min-width="130">
                <template scope="scope">
                  {{scope.row.createTime | formatTime }}
                </template>
              </el-table-column>
              <el-table-column
                prop="fromClassName"
                label="原来班级名称"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="fromLectureNo"
                label="原班讲次"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="fromTeacherName"
                label="原来任课教师"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="toCampusName"
                label="转入校区"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="toClassName"
                label="新班级名称"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="toLectureNo"
                label="新班级讲次"
                min-width="120">
              </el-table-column>
              <el-table-column
                label="上课时间"
                min-width="130">
                <template scope="scope">
                  {{ scope.row.toStartCourseTime | formatDate }} {{ scope.row.toStudyingTime }}
                </template>
              </el-table-column>
              <el-table-column
                prop="studentPhoneNo"
                label="学生电话"
                min-width="150">
              </el-table-column>

              <el-table-column
                fixed="right"
                label="操作"
                width="110">
                <template scope="scope">
                  <el-button size="small" v-if="hasPermission('cancel')"  @click.native="cancelShiftClass(scope.row)">取消调课</el-button>
                </template>
              </el-table-column>

            </el-table>
          </div>
          <div class="am-u-lg-12 am-cf">

            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="loadTableData"/>
            </div>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import moment from 'moment'
  import io from '../../lib/io'
  import Pagination from '../base/Pagination'

  export default{
    data: function () {
      return {
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNo: 1,
        query: {
          areaTeamId : window.config.areaTeams[0] && window.config.areaTeams[0].areaTeamId || '' ,
          busTeamId : '',
          periodId : '',
          gradeId:'',
          subjectId:'',
          operator : '',
          studentName: '',
          toClassName: '',
          startDate: '',
          endDate: '',
        },
        periods:[],
      }
    },
    components: {
      Pagination,
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
      this.loadPeriodData()
    },
    computed: {
      areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
          .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
          })
        return options
      },
      busTeams: function () {
        var options = ( ( this.query.areaTeamId  ) ? ( this.$root.config.groupBusTeams[this.query.areaTeamId] || [] ) : [] )
          .map(function (item) {
            return {value: item.busTeamId, text: item.name}
          })
        this.query.busTeamId = ''
        return options
      },
      grades: function () {
        return this.$root.config.grades.map(function(item){
          return {value: item.gradeId, text: item.gradeName}
        })
      },
      subjects: function () {
        return this.$root.config.subjects.map(function(item){
          return {value: item.subjectId, text: item.subjectName}
        })
      },
    },
    watch:{
      'query.areaTeamId':function(){
        this.query.busTeamId =  ''
        this.query.periodId = ''
        this.loadPeriodData()
      }
    },
    methods: {
      search: function () {
        this.loadTableData(1)
      },
      handleExport:function(){
        var _this = this
        this.query.startDate = this.query.startDate ? moment(this.query.startDate).format('YYYY-MM-DD'): ''
        this.query.endDate = this.query.endDate ? moment(this.query.endDate).format('YYYY-MM-DD'): ''
        io.downloadFile(io.exportShiftClassRecordDetail,_this.query,function(ret){
          if(ret.success){
          }else{
            _this.$alert(ret.desc)
          }
        })
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        _this.$showLoading()
        io.post(io.apiAdminAttendanceShiftRecordDetailList, $.extend({
          pageNo: _this.pageNo,
          pageSize: _this.pageSize
        }, _this.query, {startDate: this.query.startDate ? moment(this.query.startDate).format('YYYY-MM-DD'): '',endDate: this.query.endDate ? moment(this.query.endDate).format('YYYY-MM-DD'): ''}), function (ret) {
          _this.$hiddenLoading()
          if (ret.success) {
            _this.total = ret.data.total
            _this.tableData = ret.data.list
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      loadPeriodData: function () {
        var _this = this
        io.post(io.apiAdminPeriodListForAreaTeam, {
          areaTeamId : this.query.areaTeamId
        }, function (ret) {
          if (ret.success) {
            _this.periods = ret.data.map(function (item) {
              return {value: item.periodId, text: item.periodName }
            })
            _this.query.periodId = ret.data.filter(item => item.isCurrent == 1 )[0].periodId
            _this.loadTableData(_this.pageNo)
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      cancelShiftClass:function(item){
        var _this  = this
        io.post(io.apiAdminAttendanceCancelShiftClass, {
          shiftClassRecordId : item.shiftClassRecordId
        }, function (ret) {
          if (ret.success) {
            _this.loadTableData(1)
            _this.$alert('撤销成功')
          } else {
            _this.$alert(ret.desc)
          }
        })

      }
    }
  }
</script>
